home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
jwpsrc.zip
/
CLPBOARD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-31
|
28KB
|
1,061 lines
/* Copyright (C) Stephen Chung, 1991-1993. All rights reserved. */
#include "jwp.h"
static BOOL ExternalDestroy = TRUE;
static FILEOPTIONS *clipboard = NULL;
static PARAGRAPH far *curpara;
static int OriginalLineLength;
static KANJI LastDataKanji;
static int CBPos;
static BOOL CurrentlyInKanji;
static long int CurrentBlockSize;
static BYTE far *DataPtr;
static int DataPos;
static FILEPARAHEADER far *fpara = NULL;
static BOOL FormatFirstLastLine = FALSE;
static int NumberOfParagraphs = 0;
/* Variables used for JWP --> Clipboard */
static HANDLE DataHandle; /* Handle of DataPtr */
/* Variables used for Clipboard --> JWP */
static KANJI far *InsBuffer; /* Block to hold the insertion buffer */
static int InsPos;
void BlockDelete (FILEOPTIONS *f, POSITION start, POSITION stop)
{
int len;
POSITION p;
if (PARAOF(start) == PARAOF(stop)) {
DeleteString (f, start, POS2ABS(stop) - POS2ABS(start) + 1,
OP_REFORMAT | OP_UPDATE | OP_MOVESEL | OP_MINIMAL);
return;
}
p = start;
/* First delete the last portions of the first paragraph */
if (POSOF(p) > 0) {
len = unitlen(&UNITOF(p, POSOF(p)));
DeleteString (f, p, len, 0);
CHAROF(p, POSOF(p)) = '\b'; /* Join the next paragraph */
CHAROF(p, POSOF(p) + 1) = 0;
f->nr_bytes++;
PARAOF(p) = PARAOF(p)->next;
LINEOF(p) = PARAOF(p)->lines;
POSOF(p) = 0;
}
/* Delete the middle paragraphs */
for (; PARAOF(p) != PARAOF(stop); ) {
f->nr_bytes -= (unitlen(PARAOF(p)->text) - 1);
CHAROF(p, 0) = '\b';
CHAROF(p, 1) = 0;
PARAOF(p) = PARAOF(p)->next;
LINEOF(p) = PARAOF(p)->lines;
POSOF(p) = 0;
}
/* Finally we delete the first portion of the last paragraph */
/* and update the whole thing */
len = POS2ABS(stop);
if (len >= 0) {
if (CHAROF(stop,POSOF(stop))) len++;
DeleteString (f, p, len, 0);
}
/* Set the cursor position */
f->current = p;
ReformatParagraph (f, start, PARAOF(start)->next, OP_REFORMAT | OP_UPDATE | OP_MOVESEL | OP_MINIMAL);
}
void BlockReplace (FILEOPTIONS *f, POSITION start, POSITION stop, KANJI far *buf)
{
int len;
POSITION p;
if (PARAOF(start) == PARAOF(stop)) {
ReplaceString (f, start, POS2ABS(stop) - POS2ABS(start) + 1, buf,
OP_REFORMAT | OP_UPDATE | OP_MOVESEL | OP_MINIMAL);
return;
}
p = start;
/* First replace the last portions of the first paragraph */
len = unitlen(&UNITOF(p, POSOF(p)));
ReplaceString (f, p, len, buf, 0);
len = unitlen(PARAOF(p)->text);
CHAROF(p, len) = '\b'; /* Join the next paragraph */
CHAROF(p, len + 1) = 0;
f->nr_bytes++;
PARAOF(p) = PARAOF(p)->next;
LINEOF(p) = PARAOF(p)->lines;
POSOF(p) = 0;
/* Delete the middle paragraphs */
for (; PARAOF(p) != PARAOF(stop); ) {
f->nr_bytes -= (unitlen(PARAOF(p)->text) - 1);
CHAROF(p, 0) = '\b';
CHAROF(p, 1) = 0;
PARAOF(p) = PARAOF(p)->next;
LINEOF(p) = PARAOF(p)->lines;
POSOF(p) = 0;
}
/* Finally we delete the first portion of the last paragraph */
/* and update the whole thing */
len = POS2ABS(stop);
if (CHAROF(stop,POSOF(stop))) len++;
DeleteString (f, p, len, 0);
f->current = p; /* Set the cursor position */
ReformatParagraph (f, start, PARAOF(start)->next, OP_REFORMAT | OP_UPDATE | OP_MOVESEL | OP_MINIMAL);
}
static long int HowMuchData (void)
{
int i;
long int len;
PARAGRAPH far *p;
/* How much data? */
for (p = clipboard->paragraph, len = 0L; p != NULL; p = p->next) {
for (i = 0; p->text[i].kanji; i++) len += (ISKANJI(p->text[i].kanji) ? 2 : 1);
len += 1L; /* for \n */
}
return (len);
}
static HANDLE PrepareEUCData (void)
{
int i;
long int len;
HANDLE handle;
PARAGRAPH far *p;
BYTE far *cp;
if (clipboard == NULL) {
ErrorMessage(global.hwnd, "Funny! Clipboard is empty!");
return (NULL);
}
/* Allocate the data block */
len = HowMuchData() + 5L;
if (len > C64K) {
ErrorMessage(global.hwnd, "The clipboard cannot hold so much data! The limit is 64K!");
return (NULL);
}
handle = GlobalAlloc(GHND, len);
cp = (BYTE far *) GlobalLock(handle);
/* Copy data */
for (p = clipboard->paragraph; p != NULL; p = p->next) {
for (i = 0; p->text[i].kanji; i++, cp++) {
if (ISKANJI(p->text[i].kanji)) {
*cp++ = HIBYTE(p->text[i].kanji) | 0x80;
*cp = LOBYTE(p->text[i].kanji) | 0x80;
} else {
*cp = LOBYTE(p->text[i].kanji) & 0x7f;
}
}
*cp++ = '\n';
}
*cp++ = '\0';
GlobalUnlock(handle);
return (handle);
}
static int ToClipboardCharIn (void)
{
KANJI ch;
if (curpara == NULL) return (EOF);
ch = curpara->text[CBPos].kanji;
if (CurrentlyInKanji) {
CurrentlyInKanji = FALSE;
CBPos++;
return (LOBYTE(ch) | 0x80);
}
if (!ch) {
curpara = curpara->next;
CBPos = 0;
return ('\n');
} else if (ISKANJI(ch)) {
CurrentlyInKanji = TRUE;
return (HIBYTE(ch) | 0x80);
} else {
CBPos++;
return (LOBYTE(ch) & 0x7f);
}
}
static void ToClipboardCharOut (int ch)
{
if (DataPos + 5 >= CurrentBlockSize) {
/* Reallocate */
CurrentBlockSize += TEXTBLOCKSIZE;
if (CurrentBlockSize > C64K) {
ErrorMessage (global.hwnd, "The clipboard cannot hold so much data! The limit is 64K!");
return;
}
GlobalUnlock(DataHandle);
GlobalReAlloc(DataHandle, CurrentBlockSize, GHND);
DataPtr = (BYTE far *) GlobalLock(DataHandle);
}
DataPtr[DataPos++] = ch;
}
static int FromClipboardCharIn (void)
{
KANJI ch;
ch = DataPtr[DataPos++];
if (!ch) return (EOF);
return (ch);
}
static void FromClipboardCharOut (int ch)
{
KANJI ch1;
if (ch == '\r') return;
if (ch == '\n') ch = '\r';
if (CurrentlyInKanji) {
CurrentlyInKanji = FALSE;
ch1 = ((LastDataKanji << 8) | (ch & 0x7f));
} else if (ch & 0x80) {
CurrentlyInKanji = TRUE;
LastDataKanji = (ch & 0x7f);
return;
} else {
ch1 = ch;
}
if (InsPos + 5 >= CurrentBlockSize) {
/* Reallocate */
CurrentBlockSize += TEXTBLOCKSIZE;
if (CurrentBlockSize > (C64K / sizeof(KANJI))) {
ErrorMessage (global.hwnd, "The clipboard contains too much data! The limit is 64K!");
return;
}
InsBuffer = BlockRealloc(InsBuffer, CurrentBlockSize * sizeof(KANJI));
}
InsBuffer[InsPos++] = ch1;
}
static HANDLE PrepareOtherData (FILEFORMAT format)
{
int i;
long int len;
if (clipboard == NULL) {
ErrorMessage(global.hwnd, "Funny! Clipboard is empty!");
return (NULL);
}
curpara = clipboard->paragraph;
CBPos = 0;
len = HowMuchData() + 5L;
if (len > C64K) {
ErrorMessage (global.hwnd, "The clipboard cannot hold so much data! The limit is 64K!");
return (NULL);
}
i = len / TEXTBLOCKSIZE;
len = (i + 1) * TEXTBLOCKSIZE;
DataHandle = GlobalAlloc(GHND, len);
DataPtr = (BYTE far *) GlobalLock(DataHandle);
CurrentBlockSize = len;
DataPos = 0L;
CurrentlyInKanji = FALSE;
SetupIO(ToClipboardCharIn, ToClipboardCharOut);
FileExport(format);
GlobalUnlock(DataHandle);
return (DataHandle);
}
static HANDLE PrepareBitmap (void)
{
long int height, width, max;
HBITMAP hbitmap;
HDC hdc, hdcmem;
HBRUSH hbrush;
RECT rect;
PARAGRAPH far *p;
ONELINE far *lp;
BOOL OldDraftView;